package com.lyhengtongwl.zqsnews.utils;


import com.alibaba.fastjson.JSON;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.base.Constant;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class DesUtils {
    public static String desEncrypt(String source) throws Exception {
        if (source == null || source.length() == 0) {
            return null;
        }
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(Constant.DESKEY.getBytes(StandardCharsets.UTF_8));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
//        IvParameterSpec iv = new IvParameterSpec(strParam.getBytes(StandardCharsets.UTF_8));
//        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return byte2hex(
                cipher.doFinal(source.getBytes(StandardCharsets.UTF_8))).toUpperCase();
    }

    private static String byte2hex(byte[] inStr) {
        String stmp;
        StringBuilder out = new StringBuilder(inStr.length * 2);
        for (byte b : inStr) {
            stmp = Integer.toHexString(b & 0xFF);
            if (stmp.length() == 1) {
                // 濡傛灉鏄�0鑷矲鐨勫崟浣嶅瓧绗︿覆锛屽垯娣诲姞0
                out.append("0").append(stmp);
            } else {
                out.append(stmp);
            }
        }
        return out.toString();
    }

    private static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0) {
            throw new IllegalArgumentException("转换失败");
        }
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        return b2;
    }

    /**
     * 瀵圭О瑙ｅ瘑
     */
    public static String desDecrypt(String source) throws Exception {
        if (source == null || source.length() == 0) {
            return null;
        }
        byte[] src = hex2byte(source.getBytes());
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(Constant.DESKEY.getBytes(StandardCharsets.UTF_8));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
//        IvParameterSpec iv = new IvParameterSpec(strParam.getBytes(StandardCharsets.UTF_8));
//        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] retByte = cipher.doFinal(src);
        return new String(retByte);
    }


//    public static void main(String[] args) throws Exception {
//        Map<String, String> map = new HashMap<>();
//        map.put("articleId", "dsaldjs");
//        map.put("userId", "fsdjkhdfj");
//        map.put("t", System.currentTimeMillis() + "");
//        String str=JSON.toJSONString(map);
//        System.out.printf(desEncrypt(str));
////        System.out.printf(desDecrypt("330727449DAF0889"));
//
//    }
}
